// 登录后
$(window).on("load", function() {
    var oUser = JSON.parse(sessionStorage.getItem("user") || '{}');
    if(oUser.name){
        $(".head_top_unload").hide();
        $(".head_top_loading").show();
        $(".head_top_right").find(".username").text(oUser.name);

        let data = JSON.parse(localStorage.getItem("goodsInfo") || '[]');
		if(data.length == 0) {
			$(".unload-shop").show();
			$(".loading-shop").hide();
			$(".shop-total-wrap").hide();
		} else {
			$(".unload-shop").hide();
			$(".loading-shop").show();
			$(".shop-total-wrap").show();
		}
        let cartList = data.filter(item => {
			return item.userName == oUser.name;
		})
        $(".head-top-loading").find(".num").text(cartList.length);
		let checkedArr = [];
		for (let i = 0; i < cartList.length; i++){
			checkedArr.push(false)
		}
        let totalPrice = 0;

        new Vue({
				el: "#ovo",
				data: {
					cartList,
					totalPrice,
					checkedArr,
					checkedArrBtn: false,
				},
				watch: {
					cartList: {
						handler() {
							
						},
						deep: true,
					}
				},
				methods: {
					sub(index) {
						if (cartList[index].num <= 1) {
							return;
						}
						cartList[index].num--;
						if ($(".samll").eq(index)[0].checked) {
							this.totalPrice -= Number(cartList[index].price);
						}
					},
                    sup(index) {
                        if (cartList[index].num >= 5) {
							return;
						}
						cartList[index].num++
						if ($(".samll").eq(index)[0].checked) {
							this.totalPrice += Number(cartList[index].price);
						}
					},
                    checkClick(e, index) {
                        console.log(e);

						if (e.target.checked) {
							checkedArr.splice(index, 1, true)
							this.totalPrice += Number(cartList[index].num) * Number(cartList[index].price)
							$(".cart-price-red").eq(0).html(Number($(".cart-price-red").eq(0).html()) + 1)
						} else {
							checkedArr.splice(index, 1, false)
							this.totalPrice -= Number(cartList[index].num) * Number(cartList[index].price)
							$(".cart-price-red").eq(0).html(Number($(".cart-price-red").eq(0).html()) - 1)
						}
						this.checkedArrBtn = checkedArr.every(item => {
							return item == true
						})
					},
					del(index) {
						if (confirm("是否要从购物车中移除该商品?")) {
                            if (checkedArr[index]) {
                                this.totalPrice -= Number(cartList[index].num) * Number(cartList[index].price);
                            }
                            let delList = cartList.slice(index, index + 1);
							for(let i = 0; i < data.length; i++){
								if(data[i] == delList[0]){
									data.splice(i, 1);
								}
							}
							cartList.splice(index, 1);
							if(data.length == 0) {
								$(".unload-shop").show();
								$(".loading-shop").hide();
								$(".shop-total-wrap").hide();
							} else {
								$(".unload-shop").hide();
								$(".loading-shop").show();
								$(".shop-total-wrap").show();
							}
							localStorage.setItem("goodsInfo", JSON.stringify(data))
                            checkedArr.splice(index, 1);
						}
					},
					checkAll(e) {
						if (e.target.checked) {
							this.checkedArr.forEach( (item, index) => {
								this.checkedArr.splice(index, 1, true)
							})
							this.totalPrice = 0;
							cartList.forEach(item => {
								this.totalPrice += Number(item.num) * Number(item.price)
							})
							// $(".cart-price-red").eq(0).html(this.cartList.length)
						} else {
							this.checkedArr.forEach( (item, index) => {
								this.checkedArr.splice(index, 1, false)
							})
							this.totalPrice = 0;
							// $(".cart-price-red").eq(0).html(0)
						}
					},
					delAll() {
						if (confirm("是否删除所有选中商品?")) {
							for (let i = this.checkedArr.length - 1; i >= 0; i--){
								if (this.checkedArr[i]) {
									this.totalPrice -= Number(cartList[i].num) * Number(cartList[i].price);
									this.cartList.splice(i, 1);
									this.checkedArr.splice(i, 1);
									$(".cart-price-red").eq(0).html(Number($(".cart-price-red").eq(0).html()) - 1)
								}	
							}
						}
						
					}
				}
			})
    } 
})

// 退出
$(function() {
    $(".head-top-loading").find(".quit").on("click", function () {
        if (!confirm('你确定要退出吗?')) {
            return false;
        }
        sessionStorage.clear();
        location = "../login/login.html";
    })
})